package com.itheima.controller.backend;

import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Encounter
 * @date 2024/10/03 18:04<br/>
 */
@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController
    {
        @Autowired
        private CheckGroupService checkGroupService;
        
        /**
         * 新增检查组
         *
         * @param checkItemIds 检查项目 ID
         * @param checkGroup   检查组
         * @return {@link Result }
         */
        @PostMapping("/add")
        public Result add(@RequestParam List<Integer> checkItemIds, @RequestBody CheckGroup checkGroup)
            {
                try
                    {
                        log.info("检查项id：{}，添加检查组：{}", checkItemIds, checkGroup);
                        checkGroupService.add(checkItemIds, checkGroup);
                        //添加成功
                        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //添加失败
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
                    }
            }
        
        /**
         * 删除由id
         *
         * @param checkItemIds 检查项目 ID
         * @return {@link Result }
         */
        @DeleteMapping("/delete")
        public Result deleteById(@RequestParam List<Integer> checkItemIds)
            {
                try
                    {
                        log.info("要删除的id：{}", checkItemIds);
                        checkGroupService.delete(checkItemIds);
                        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
                    }
            }
        
        /**
         * 找到由id
         *
         * @param checkGroupId 检查群id
         * @return {@link Result }
         */
        @GetMapping("/findById")
        public Result findById(@RequestParam Integer checkGroupId)
            {
                log.info("要查询的id:{}", checkGroupId);
                CheckGroup checkGroup = checkGroupService.getById(checkGroupId);
                if (checkGroup != null)
                    {
                        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);
                    }
                else
                    {
                        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
                    }
            }
        
        /**
         * 修改检查组
         *
         * @param checkGroup 检查组
         * @return {@link Result }
         */
        @PutMapping("/edit")
        public Result edit(@RequestBody CheckGroup checkGroup)
            {
                try
                    {
                        log.info("修改checkGroup:{}", checkGroup);
                        checkGroupService.update(checkGroup);
                        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
                    }
            }
        
        /**
         * 分页查询
         *
         * @param queryPageBean 查询页面 Bean
         * @return {@link Result }
         */
        @PostMapping("/findPage")
        public Result queryPage(@RequestBody QueryPageBean queryPageBean)
            {
                try
                    {
                        log.info("分页查询参数queryPageBean:{},{}", queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
                        PageResult pageResult = checkGroupService.queryPage(queryPageBean);
                        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, pageResult);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
                    }
            }
        
        /**
         * 查找全部
         *
         * @return {@link Result }
         */
        @GetMapping("/findAll")
        public Result findAll()
            {
                try
                    {
                        log.info("查询全部");
                        List<CheckGroup> all = checkGroupService.findAll();
                        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, all);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
                    }
            }
        
        /**
         * 找到检查群IDS由套餐id
         *
         * @param setmealId 套餐id
         * @return {@link Result }
         */
        @GetMapping("/findCheckGroupIdsBySetmealId")
        public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId)
            {
                try
                    {
                        log.info("套餐id：{}", setmealId);
                        List<Integer> checkGroupIds =checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
                        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupIds);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
                    }
            }
    }
